package com.github.benmanes.caffeine.demo.referrence;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.checkerframework.checker.lock.qual.NewObject;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.io.IOException;

public class DemoWeakRefProblem {
    private static  int NO = 0;
    static System.Logger logger = System.getLogger(DemoWeakRefProblem.class.getName());

    public static void hello(String[] args) {
        System.out.println("args = " + args);
    }


    public static void main(String... args) throws Exception {

        sameString();


        Cache<String, String> cache = Caffeine.newBuilder()
                .weakKeys()
                .softValues()
                .build();



        for (int i = 0; i < 100; i++) {

            String key =new String( "疯狂创客圈");

            //当key不存在时，会立即创建出对象来返回，val 不会为空
            String  val = cache.get(key, k -> {
                System.out.println("key:" + k);
                return "一个高并发 研究社群 count:"+ ++NO;
            });
            System.out.println(val);
        }

    }
    public static void sameString() throws IOException {
        System.out.println(System.identityHashCode(new String("疯狂创客圈")));
        System.out.println(System.identityHashCode(new String("疯狂创客圈")));
    }
}
